/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.ts.controller;

import java.util.Collection;
import java.util.List;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.biz.ts.domain.resp.GetExaminationRecordQuestionResponse;
import com.xy.biz.ts.domain.req.GetExaminationRecordQuestionRequest;
import com.xy.biz.ts.domain.req.ListExaminationRecordQuestionRequest;
import com.xy.biz.ts.domain.req.SaveExaminationRecordQuestionRequest;
import com.xy.biz.ts.domain.query.ExaminationRecordQuestionQuery;
import com.xy.biz.ts.domain.converter.ExaminationRecordQuestionConverter;
import com.xy.biz.ts.client.ExaminationRecordQuestionClient;
import com.xy.biz.ts.service.ExaminationRecordQuestionService;

/**
 *
 *
 * @author yzh
 * @date 2021-4-6
 */
@Slf4j
@RestController
public class ExaminationRecordQuestionController implements ExaminationRecordQuestionClient {

	@Autowired
	private ExaminationRecordQuestionService examinationRecordQuestionService;

	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save")
	public Result<?> saveExaminationRecordQuestion(@RequestBody SaveExaminationRecordQuestionRequest request) {
		examinationRecordQuestionService.saveExaminationRecordQuestion(ExaminationRecordQuestionConverter.toBO(request));
		return Result.success();
	}

	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save_all")
	public Result<?> saveAllExaminationRecordQuestion(@RequestBody Collection<SaveExaminationRecordQuestionRequest> collection) {
		examinationRecordQuestionService.saveAllExaminationRecordQuestion(ExaminationRecordQuestionConverter.toBO(collection));
		return Result.success();
	}

	@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/update")
	public Result<?> updateExaminationRecordQuestion(@RequestBody SaveExaminationRecordQuestionRequest request) {
		examinationRecordQuestionService.updateExaminationRecordQuestion(ExaminationRecordQuestionConverter.toBO(request));
		return Result.success();
	}

	@Validatable
	@PostMapping(value = "/delete")
	public Result<?> deleteExaminationRecordQuestion(@RequestBody GetExaminationRecordQuestionRequest request) {
		examinationRecordQuestionService.deleteExaminationRecordQuestion(request.getExaminationRecordQuestionId());
		return Result.success();
	}

	@Validatable
	@PostMapping(value = "/get")
	@Override
	public Result<GetExaminationRecordQuestionResponse> getExaminationRecordQuestion(@RequestBody GetExaminationRecordQuestionRequest request) {
		return Result.success(ExaminationRecordQuestionConverter.toResponse(examinationRecordQuestionService.getExaminationRecordQuestion(request.getExaminationRecordQuestionId())));
	}

	@PostMapping(value = "/list_by_ids")
	@Override
	public Result<List<GetExaminationRecordQuestionResponse>> listExaminationRecordQuestionById(@RequestBody Collection<Long> collection) {
		return Result.success(ExaminationRecordQuestionConverter.toResponse(examinationRecordQuestionService.listExaminationRecordQuestionById(collection)));
	}

	@PostMapping(value = "/list")
	@Override
	public Result<List<GetExaminationRecordQuestionResponse>> listExaminationRecordQuestion(@RequestBody ListExaminationRecordQuestionRequest request) {
		return Result.success(ExaminationRecordQuestionConverter.toResponse(examinationRecordQuestionService.listExaminationRecordQuestion(ExaminationRecordQuestionConverter.toQuery(request))));
	}

	@PostMapping(value = "/count")
	@Override
	public Result<Integer> countExaminationRecordQuestion(@RequestBody ListExaminationRecordQuestionRequest request) {
		return Result.success(examinationRecordQuestionService.countExaminationRecordQuestion(ExaminationRecordQuestionConverter.toQuery(request)));
	}

	@PostMapping(value = "/page")
	@Override
	public Result<Page<GetExaminationRecordQuestionResponse>> pageExaminationRecordQuestion(@RequestBody ListExaminationRecordQuestionRequest request) {
		ExaminationRecordQuestionQuery query = ExaminationRecordQuestionConverter.toQuery(request);
		List<GetExaminationRecordQuestionResponse> list = ExaminationRecordQuestionConverter.toResponse(examinationRecordQuestionService.listExaminationRecordQuestion(query));
		int count = examinationRecordQuestionService.countExaminationRecordQuestion(query);
		return Result.success(new Page<>(list, count));
	}

}
